﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimulateDbData
{
    public partial class Form1 : Form
    {
        DataTable fyTable;
        DataTable tsTable;
        DataTable ryTable;
        DB2Service.DB2ServiceClass dbService;
        string connection;
        public Form1()
        {
            InitializeComponent();
            connection = "Database=LZZYLW;User ID=GXNETWORK;Password=rX^Lw%nw3*;Server=147.32.3.248:50000";
            dbService=new DB2Service.DB2ServiceClass();
            fyTable = ((DBService.IDBService)dbService).Select("select DM,MC from B_FY WHERE MC LIKE '%柳%'", connection);
            tsTable = ((DBService.IDBService)dbService).Select("SELECT FY AS FYDM,XH AS DM,MC  FROM GXNETWORK.B_TSJG WHERE SFTJFLAG=1", connection);
            ryTable = ((DBService.IDBService)dbService).Select("SELECT XCBM AS TSDM ,DM,MC FROM B_RYXX", connection);
            
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            int count = int.Parse(textBox1.Text);
            string txt = textBox2.Text;
            string[] tablenames = ExSystem.ExString.SplitTrim(txt, ",");
            foreach (string tablename in tablenames)
            {

                DataTable dt = ((DBService.IDBService)dbService).Select(string.Format("select COLUMN_NAME,Data_TYpe,character_maximum_length,Character_octet_length,NUMeric_precision,numeric_precision_radix from SYSIBM.COLUMNS where Table_name='{0}' and TABLE_SCHEMA='GXNETWORK' order by ORDINAL_POSITION", tablename), connection);
                dt.TableName = tablename;
                string insertSql = creatInsertSql(dt);
                DBService.DBParameterCollentions collection;
                for (int i = 0; i < count; i++)
                {
                    try
                    {
                        collection = createDBParameter(dt);
                        dbService.ExecuteNonQuery(insertSql, connection, collection);
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                }
            
            }
        }

        private DBService.DBParameterCollentions createDBParameter(DataTable dt)
        {
            Random random=new Random();
            DataRow[] rows;
            DBService.DBParameterCollentions collection = new DBService.DBParameterCollentions();
            DBService.DBParameter para;
            string fydm=fyTable.Rows[random.Next(0,fyTable.Rows.Count-1)]["DM"].ToString();
            rows=fyTable.Select("DM="+fydm);
            string fymc="";
            if (rows.Length > 0) fymc = rows[0]["MC"].ToString();
            
             rows=tsTable.Select("FYDM="+fydm);
             string tsdm = "";
             if (rows.Length > 0) tsdm = rows[random.Next(0, rows.Length - 1)]["DM"].ToString();
            rows=tsTable.Select("DM="+tsdm);
            string tsmc = "";
            if (rows.Length > 0) tsmc = rows[0]["MC"].ToString();
            rows=ryTable.Select("TSDM="+tsdm);
            string rymc = "";
            if (rows.Length > 0) rymc = rows[random.Next(0, rows.Length - 1)]["MC"].ToString();
            foreach (DataRow row in dt.Rows)
            {
                string columname=row["COLUMN_NAME"].ToString().Trim();
                para = new DBService.DBParameter();
                para.ColumnName=columname;
                para.Direction = ParameterDirection.Input;
                switch (columname.ToLower())
                {
                    case "fydm":
                        para.value = fydm;
                        break;
                    case "xh":
                        para.value = tsdm; break;
                    case "cbts":
                        para.value = tsmc; break;
                    case "cbr":
                        para.value = rymc; break;
                    case "ahyear":
                        para.value = random.Next(2000, 2013); break;
                    case "ahz":
                        para.value = "执"; break;
                    case "ahzt":
                        para.value = "柳市"; break;
                    case "ahh":
                        para.value = random.Next(1, 300); break;
                    default:
                        para.value = CreateValue(row["Data_TYpe"].ToString().Trim());
                        break;
                
                }
                collection.AddDBParameter(para);
            
            }
            return collection;
            
        }
        private void Insert_()
        { 
            
        }
        private object CreateValue(string dataTypeString)
        {
            Random r = new Random();
            
            switch (dataTypeString)
            { 

                case "INTEGER":
                    return r.Next(100);
                    break;
                case "DOUBLE PRECISION":
                   return r.Next(100);
                    break;
                case "CHARACTER VARYING":
                     return r.Next(100).ToString();
                    break;
                case "DATE":
                    return string.Format("{0}-{1}-{2}", r.Next(2000, 2013), r.Next(1, 12), r.Next(1, 28));
                    break;
                default :
                    return null;
                    break; 
            
            }
        }
        private string creatInsertSql(DataTable dt)
        {
            StringBuilder b = new StringBuilder();
            StringBuilder b2 = new StringBuilder();
            string paras="";
            string paras2 = "";
            foreach (DataRow row in dt.Rows)
            {
                b.Append(string.Format("@{0},", row["COLUMN_NAME"].ToString()));
                b2.Append(string.Format("{0},", row["COLUMN_NAME"].ToString()));
            }
            paras = ExSystem.ExString.RemoveLastString(b.ToString(), ",");
            paras2 = ExSystem.ExString.RemoveLastString(b2.ToString(), ",");
            return string.Format("insert into {0} ({2}) values({1})", dt.TableName, paras, paras2);
        }

        
    }
}
